﻿using System.Collections.Generic;
using System.Linq;

namespace Tuxedo.Compile
{
	internal class AdditiveExpression : Expression
	{
		public Expression[] Expressions { get; private set; }
		public char[] Ops { get; private set; }

		public AdditiveExpression(IList<Expression> expressions, IList<string> ops)
			: base()
		{
			this.SimplifyConstantValues(expressions, ops);

			char[] charOps = new char[ops.Count];
			for (int i = 0; i < ops.Count; ++i)
			{
				charOps[i] = ops[i][0];
			}
			this.Ops = charOps;

			this.Expressions = expressions.ToArray();

			if (this.Ops.Length == 0)
			{
				this.EffectiveValue = this.Expressions[0];
			}
		}

		protected override Expression DoConstantSimplification(Constant left, Constant right, string op)
		{
			if (op == "-")
			{
				return left.Subtract(right);
			}

			return left.Add(right);
		}
	}
}
